package com.darthsith.droidathome.core.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.darthsith.droidathome.core.dao.UserDao;
import com.darthsith.droidathome.core.model.User;

@Repository
public class UserDaoImpl implements UserDao {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public void addUser(User user) {
		sessionFactory.getCurrentSession().save(user);	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> listAll() {
		return sessionFactory.getCurrentSession().createQuery("FROM USER").list();
	}

	@Override
	public void removeUser(User user) {
		String username=user.getUsername();
		User u=(User) sessionFactory.getCurrentSession().load(User.class, username);
		if (u!=null)
			sessionFactory.getCurrentSession().delete(u);
	}

	@Override
	public User getUser(String username) {
		return (User) sessionFactory.getCurrentSession().load(User.class, username);
	}
}
